﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using Logistics.DataManage;

namespace Logistics.Win
{
    /// <summary>
    /// Interaction logic for DataUploadWin.xaml
    /// </summary>
    public partial class DataUploadWin : Window
    {
        public DataUploadWin()
        {
            InitializeComponent();
            InitWin();
            
        }
        private void InitWin()
        {
            DataUploader Uploader = new DataUploader();
            int count = Uploader.GetUnUploadLocalBillCount();
            if (count == 0)
            {
                this.txtMessage.Text = "当前没有需要上传的运单信息";
                this.BtnStart.IsEnabled = false;
            }
            else
            {
                this.txtMessage.Text = "当前有" + count + "条运单信息等待上传";
                this.BtnStart.IsEnabled = true;
            }
        }
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            this.StartProcess();
        }

        private void StartProcess()
        {
            bool Success = false;
            String msg=null;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, ea) =>
            {
                try
                {
                    //for (int i = 0; i < 100; i++)
                    //{
                    //    System.Threading.Thread.Sleep(30);
                    //}
                    DataUploader Uploader = new DataUploader();
                    bool result = Uploader.UploadWayBills(out msg);
                    Success = result;
                }
                catch(Exception ex)
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        this.txtMessage.Text = "操作发生异常:" + ex.Message;
                    }));
                }
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                _busyIndicator.IsBusy = false;
                if (Success)
                {
                    this.txtMessage.Text = "上传完毕!";
                }
                else
                {
                    this.txtMessage.Text = msg;
                }
                this.BtnStart.IsEnabled = false;
            };
            
            //set the IsBusy before you start the thread
            _busyIndicator.IsBusy = true;
            worker.RunWorkerAsync();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (this._busyIndicator.IsBusy)
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("提示","请勿在上传时关闭当前窗口");
                e.Cancel = true;
            }
        }
        public void DragWindow(object sender, MouseButtonEventArgs args)
        {
            this.DragMove();
        }

        public void CloseWindow(object sender, RoutedEventArgs args)
        {
            this.Close();
        }
    }
}
